import { any } from "../../../core/logic";
import { Behavior } from "./Behavior";
import { Event } from "../../../event/index";
import { getEventPos } from "../../../core/utils";
import { browser } from "../../../core/browser";
import { Engine } from "../../../Engine";
/**
 *鼠标行为的父类
 * @class
 * @extends Behavior
 */
class Mouse extends Behavior {
    /**
   *鼠标行为父类构造函数
   * @constructor
   * @param {*} opts 
   * @param {Boolean} opts.ctrl 【可选】ctrl键是否按下。缺省值:false。
   * @param {Boolean} opts.shift 【可选】alt键是否按下。缺省值:false。
   * @param {Boolean} opts.alt 【可选】shift键是否按下。缺省值:false。
   */
    static instance = null;
    constructor(opts) {
        if (Mouse.instance) {
            return Mouse.instance;
        }
        super(opts);
        this.ctrl = any(opts.ctrl, false);
        this.shift = any(opts.shift, false);
        this.alt = any(opts.alt, false);
        this.clickTimeOut = null;
        this.pointer = [0, 0];
        this.clickCount = 0;
        Mouse.instance = this;
    }
    get ctrl() {
        return this._ctrl;
    }
    set ctrl(v) {
        this._ctrl = v;
        return this;
    }
    get shift() {
        return this._shift;
    }
    set shift(v) {
        this._shift = v;
        return this;
    }
    get alt() {
        return this._alt;
    }
    set alt(v) {
        this._alt = v;
        return this;
    }
    active() {
        this.origin.addEventListener("click", this.click);
        // this.origin.addEventListener("dblclick", this.dblclick);
        this.origin.addEventListener("mousedown", this.down);
        this.origin.addEventListener("mousemove", this.move);
        this.origin.addEventListener("mouseup", this.up);
        this.origin.addEventListener("mouseout", this.out);
        this.origin.addEventListener('wheel', this.wheel);
    }
    deactive() {
        this.origin.removeEventListener("click", this.click);
        // this.origin.removeEventListener("dblclick", this.dblclick);
        this.origin.removeEventListener("mousedown", this.down);
        this.origin.removeEventListener("mousemove", this.move);
        this.origin.removeEventListener("mouseup", this.up);
        this.origin.removeEventListener("mouseout", this.out);
        this.origin.removeEventListener('wheel', this.wheel);
    }
    click(event) {//注释部分是放开双击，不建议支持双击
        if (Mouse.instance.hasMove) {
            return;
        }
        event.preventDefault();
        event.stopPropagation();
        Mouse.instance.clickCount++;
        if (Mouse.instance.clickCount == 1) {
            Mouse.instance.delay=setTimeout(function () {
                if (Mouse.instance.clickCount == 1) {
                    let e = new Event("click");
                    e.originEvent = event;
                    e.data = {
                        pixel: getEventPos(event),
                        time: browser.now() / 1000,
                        center: [Mouse.instance.origin.width / 2, Mouse.instance.origin.height / 2],
                        button: event.button,
                    }
                    Mouse.instance.clickCount--;
                    Mouse.instance.publish(e);
                }
            }, 200)
        }else if(Mouse.instance.clickCount==2){
            clearTimeout(Mouse.instance.delay);
            Mouse.instance.clickCount=0;
            let e = new Event("dblclick");
            e.originEvent = event;
            e.data = {
                pixel: getEventPos(event),
                time: browser.now() / 1000,
                center: [Mouse.instance.origin.width / 2, Mouse.instance.origin.height / 2],
                button: event.button,
            }
            Mouse.instance.publish(e);
        }

    }
    down(event) {
        Mouse.instance.hasMove = false;
        Mouse.instance.pointer = getEventPos(event);
        event.preventDefault();
        let e = new Event("mousedown");
        e.originEvent = event;
        e.data = {
            pixel: Mouse.instance.pointer,
            time: browser.now() / 1000,
            center: [Mouse.instance.origin.width / 2, Mouse.instance.origin.height / 2],
            button: event.button,
        }
        Mouse.instance.publish(e);
        //修改光标
        if (Engine.instance.closecursor) {
            Engine.instance.canvas.style.cursor = `url(${Engine.instance.closecursor}),auto`;
        }
    }
    move(event) {
        let pointer = getEventPos(event);
        if (Math.abs(pointer[0] - Mouse.instance.pointer[0]) + Math.abs(pointer[1] - Mouse.instance.pointer[1]) > 5) {
            Mouse.instance.hasMove = true;
        }
        event.preventDefault();
        let e = new Event("mousemove");
        e.originEvent = event;
        e.data = {
            pixel: pointer,
            time: browser.now() / 1000,
            center: [Mouse.instance.origin.width / 2, Mouse.instance.origin.height / 2],
            button: event.button,
        }
        Mouse.instance.publish(e);
    }
    up(event) {
        event.preventDefault();
        let e = new Event("mouseup");
        e.originEvent = event;
        e.data = {
            pixel: getEventPos(event),
            time: browser.now() / 1000,
            center: [Mouse.instance.origin.width / 2, Mouse.instance.origin.height / 2],
            button: event.button,
        }
        Mouse.instance.publish(e);
        //修改光标
        if (Engine.instance.opencursor) {
            Engine.instance.canvas.style.cursor = `url(${Engine.instance.opencursor}),auto`;
        }
    }
    out(event) {
        event.preventDefault();
        Mouse.instance.up(event);
        let e = new Event("mouseout");
        e.originEvent = event;
        e.data = {
            pixel: getEventPos(event),
            time: browser.now() / 1000,
            center: [Mouse.instance.origin.width / 2, Mouse.instance.origin.height / 2],
            button: event.button,
        }
        Mouse.instance.publish(e);
    }
    wheel(event) {
        event.preventDefault();
        let e = new Event("wheel");
        e.originEvent = event;
        e.data = {
            pixel: getEventPos(event),
            zoom: Math.sign(event.wheelDeltaY)
        }
        Mouse.instance.publish(e);
    }
}
export { Mouse }